/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "S_纹理.h"

#define STB_IMAGE_IMPLEMENTATION

#include <omp.h>
#include <stb_image.h>

#include "intern/Vulkan/buffer/Vk图像.h"
#include "底层绘图/底层绘图框架.h"
#include "纹理/纹理.h"



static uint32 f_转VK纹理用途(uint32 用途) {
	uint32 usage = 0;
	if (用途 & E_纹理用途::e_采样)			usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
	if (用途 & E_纹理用途::e_转移源)		usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
	if (用途 & E_纹理用途::e_转移目标)		usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
	if (用途 & E_纹理用途::e_贮存)			usage |= VK_IMAGE_USAGE_STORAGE_BIT;
	if (用途 & E_纹理用途::e_颜色附件)		usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
	if (用途 & E_纹理用途::e_深度模板附件)	usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
	if (用途 & E_纹理用途::e_短时附件)		usage |= VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
	if (用途 & E_纹理用途::e_输入附件)		usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
	//if (用途 & E_纹理用途::e_采样) usage |= VK_IMAGE_USAGE_INVOCATION_MASK_BIT_HUAWEI;
	//if (用途 & E_纹理用途::e_采样) usage |= VK_IMAGE_USAGE_SHADING_RATE_IMAGE_BIT_NV;
	return usage;
}





S_纹理::~S_纹理() {
	if (m_像素 && m_其它库加载纹理) {
		stbi_image_free(m_像素);
	}
}



S_纹理* f_tex_创建图像纹理(S_设备环境& ctx, uvec3 size, E_纹理格式 格式, E_纹理维度类型 类型, E_MS次数 采样, E_纹理排列 平铺方式) {
	S_VkImage* image = new S_VkImage();

	auto device = f_vk_getDevice(ctx);
	VkImageUsageFlags textureUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
	uint32 用途 = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;

	f_df_钳制纹理大小(ctx, size);

	image->m_Image = f_tex_创建图像(device, f_vk_getVK图像格式(格式), 类型, size, 用途, 采样);
	image->m_Ctx = ctx;

	f_tex_分配图像内存(ctx, *image);
	f_tex_创建图像视图(device, *image, VK_IMAGE_ASPECT_COLOR_BIT, f_vk_getVK图像格式(格式), 类型, 1, VK_IMAGE_LAYOUT_GENERAL);

	image->m_格式 = 格式;
	image->m_类型 = 类型;
	image->m_Size = size;
	image->m_用途 = 用途;
	
	return image;
}

S_纹理* f_tex_创建深度纹理(S_设备环境& ctx, uvec3 size, E_MS次数 采样, E_纹理维度类型 类型, E_纹理格式 格式) {
	auto* dc = f_vk_get绘图环境(ctx);
	uint32 用途 = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;

	f_df_钳制纹理大小(ctx, size);

	S_VkImage* 图像 = S_Vk图像管理::f_创建深度缓存图像(dc->m_设备, dc->m_内存属性, dc->m_设备属性, f_vk_getVK图像格式(格式), size, 采样, 类型);
	图像->m_格式 = 格式;
	图像->m_类型 = 类型;
	图像->m_Size = size;
	图像->m_用途 = 用途;
	图像->m_Ctx = ctx;

	图像->m_通道数量 = f_tex_格式通道数量(格式);
	return 图像;
}

S_纹理* f_tex_创建帧缓存纹理(S_设备环境& ctx, uvec3 size, E_纹理格式 格式, E_纹理维度类型 类型, E_MS次数 采样, E_纹理排列 平铺方式, uint32 层) {
	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	auto* 绘图环境 = f_vk_get绘图环境(ctx);


	f_df_钳制纹理大小(ctx, size);

	if (层 > 1) {
		size.x = (size.x + 1) & 0xfffffff0;
		size.y = (size.y + 1) & 0xfffffff0;
	}

	uint32 用途 = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_STORAGE_BIT;

	S_VkImage* 图像 = S_Vk图像管理::f_创建帧缓存图像(逻辑设备, 绘图环境->m_内存属性, f_vk_getVK图像格式(格式), 类型, size, 采样, 用途);
	图像->m_格式 = 格式;
	图像->m_类型 = 类型;
	图像->m_Size = size;
	图像->m_用途 = 用途;
	图像->m_Ctx = ctx;
	图像->m_Mip = 层;

	图像->m_通道数量 = f_tex_格式通道数量(格式);
	return 图像;
}

S_纹理* f_tex_创建存储2D纹理(S_设备环境& ctx, uvec3 size, E_纹理格式 格式, uint32 用途, E_MS次数 采样) {
	auto* 绘图环境 = f_vk_get绘图环境(ctx);
	f_df_钳制纹理大小(ctx, size);

	S_VkImage* 图像 = S_Vk图像管理::f_创建纹理图像(ctx, f_vk_getVK图像格式(格式), size, E_纹理维度类型::e_2D, f_转VK纹理用途(用途), 采样);
	图像->m_格式 = 格式;
	图像->m_类型 = E_纹理维度类型::e_2D;
	图像->m_Size = size;
	图像->m_用途 = f_转VK纹理用途(用途);
	图像->m_Ctx = ctx;

	图像->m_通道数量 = f_tex_格式通道数量(格式);

	auto cmd = f_vk创建命令缓存(绘图环境);
	f_vk绑定命令缓存(绘图环境, cmd);
	f_vkImage_setLayout(cmd, 图像->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1);
	图像->m_纹理信息.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
	f_vk刷新命令缓存(绘图环境, cmd, true);

	return 图像;
}

S_纹理* f_tex_创建帧缓存图像(S_设备环境& ctx, uvec3 size, E_纹理格式 格式, E_MS次数 采样, uint32 mip) {
	S_VkImage* image = new S_VkImage();

	f_df_钳制纹理大小(ctx, size);

	auto device = f_vk_getDevice(ctx);
	image->m_用途 = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
	image->m_类型 =  E_纹理维度类型::e_2D;
	image->m_格式 = 格式;
	image->m_采样 = 采样;
	image->m_ImageAspect = VK_IMAGE_ASPECT_COLOR_BIT;
	image->m_Size = size;
	image->m_Mip = mip;

	image->m_Image = f_tex_创建图像(device, f_vk_getVK图像格式(格式), image->m_类型, size, image->m_用途, 采样, mip);
	image->m_Ctx = ctx;
	
	f_tex_分配图像内存(ctx, *image);
	f_tex_创建图像视图(device, *image, image->m_ImageAspect, f_vk_getVK图像格式(格式), image->m_类型, 1, VK_IMAGE_LAYOUT_GENERAL);

	return image;
}

void f_tex_销毁纹理(S_纹理* tex) {
	S_VkImage* vt = dynamic_cast<S_VkImage*>(tex);
	f_releaseImage(f_vk_getDevice(tex->m_Ctx), vt);
	delete tex;
}

void f_tex_创建屏幕呈现图像(S_纹理* tex, VkImage image) {
	S_VkImage* vkTex = dynamic_cast<S_VkImage*>(tex);

	vkTex->m_Mem = nullptr;
	vkTex->m_View = 0;
	vkTex->m_Image = image;
	vkTex->m_ImageAspect = VK_IMAGE_ASPECT_COLOR_BIT;
	//f_tex_创建图像视图(dev, *vkTex, VK_IMAGE_ASPECT_COLOR_BIT, 格式, E_纹理维度类型::e_2D, 1);
	//iObj->m_布局 = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}


S_纹理采样器* f_tex_创建纹理采样器(S_设备环境& ctx, S_纹理采样器创建参数 参数) {
	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	S_Vk纹理采样器* sample = new S_Vk纹理采样器(逻辑设备, 参数);
	sample->m_Ctx = ctx;
	return sample;
}

S_纹理采样器* f_tex_创建阴影纹理采样器(S_设备环境& ctx, S_纹理采样器创建参数 参数) {
	参数.U = E_纹理拉伸方式::e_边缘;
	参数.V = E_纹理拉伸方式::e_边缘;
	参数.W = E_纹理拉伸方式::e_边缘;

	参数.m_预设预设 = E_预设颜色::e_白色;

	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	S_Vk纹理采样器* sample = new S_Vk纹理采样器(逻辑设备, 参数);
	sample->m_Ctx = ctx;
	return sample;
}

S_纹理采样器* f_tex_创建颜色纹理采样器(S_设备环境& ctx) {

	S_纹理采样器创建参数 参数{};

	参数.U = E_纹理拉伸方式::e_边缘;
	参数.V = E_纹理拉伸方式::e_边缘;
	参数.W = E_纹理拉伸方式::e_边缘;

	参数.m_预设预设 = E_预设颜色::e_白色;

	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	S_Vk纹理采样器* sample = new S_Vk纹理采样器(逻辑设备, 参数);
	sample->m_Ctx = ctx;
	return sample;
}

S_纹理采样器* f_tex_创建平铺纹理采样器(S_设备环境& ctx) {

	S_纹理采样器创建参数 参数{};

	参数.U = E_纹理拉伸方式::e_重复;
	参数.V = E_纹理拉伸方式::e_重复;
	参数.W = E_纹理拉伸方式::e_重复;

	参数.m_预设预设 = E_预设颜色::e_白色;

	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	S_Vk纹理采样器* sample = new S_Vk纹理采样器(逻辑设备, 参数);
	sample->m_Ctx = ctx;
	return sample;
}





void f_tex_销毁纹理采样器(S_纹理采样器* 采样器) {
	delete 采样器;
}

void f_tex_安装纹理采样器(S_纹理* tex, S_纹理采样器* sample) {
	S_VkImage* 图像 = (S_VkImage*)(tex);

	tex->m_采样器 = sample;
	图像->m_纹理信息.sampler = dynamic_cast<S_Vk纹理采样器*>(sample)->m_采样器;

	//switch (tex->m_类型) {
	//	//case e_1D:
	//	//case e_2D:
	//	//case e_3D:
	//	//case e_1D_Array:
	//	//case e_2D_Array: {
	//	//	break;
	//	//}
	//	case e_CUBE:
	//	case e_CUBE_Array: {
	//		for (uint32 i = 0; i < 6; ++i) {
	//			图像->m_Cube纹理信息[i].sampler = dynamic_cast<S_Vk纹理采样器*>(sample)->m_采样器;
	//		}
	//		break;
	//	}
	//	default:
	//		break;
	//}
	
}




void f_tex_填充纹理数据(S_纹理* tex, const uint8* t, uint16 offset, uvec3 dim, S_结构指针 命令缓存, bool 深度纹理) {
	assert(dynamic_cast<S_VkImage*>(tex));
	S_VkImage* image = (S_VkImage*)(tex);
	
	uint64 num = dim.x * dim.y * dim.z;

	S_VkArray缓存* 临时缓存 = dynamic_cast<S_VkArray缓存*>(f_buf_create板载缓存(tex->m_Ctx, offset, E_板载缓存类型::e_TBOs, num));
	f_buf_fill板载缓存(临时缓存, t, num);
	
	tex->m_格式;
	VkBufferImageCopy bufferCopyRegion = {}; //构建缓冲图像拷贝结构体实例
	bufferCopyRegion.imageSubresource.aspectMask = 深度纹理 ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
	bufferCopyRegion.imageSubresource.mipLevel = 0;								//mipmap 级别
	bufferCopyRegion.imageSubresource.baseArrayLayer = 0;						//基础数组层
	bufferCopyRegion.imageSubresource.layerCount = dim.z;						//数组层的数量
	bufferCopyRegion.imageExtent.width = dim.x;
	bufferCopyRegion.imageExtent.height = dim.y;
	bufferCopyRegion.imageExtent.depth = 1;



	S_VK渲染环境* 配置 = (S_VK渲染环境*)(tex->m_Ctx.m_绘图环境);
	VkCommandBuffer cmd_bufs[] = { (VkCommandBuffer)命令缓存 };
	if (!命令缓存) cmd_bufs[0] = 配置->m_命令缓存;
	
	

	VkFenceCreateInfo fenceCreateInfo{};
	fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
	fenceCreateInfo.flags = 0;
	VkFence fence;
	vkCreateFence(配置->m_设备, &fenceCreateInfo, nullptr, &fence);


	static VkCommandBufferInheritanceInfo InheritanceInfo{};
	InheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
	vkResetCommandBuffer(cmd_bufs[0], 0);
	VkCommandBufferBeginInfo cmdBufferBeginInfo{};
	cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
	cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
	cmdBufferBeginInfo.pInheritanceInfo = &InheritanceInfo;
	auto err = vkBeginCommandBuffer(cmd_bufs[0], &cmdBufferBeginInfo);
	

	f_vkImage_setLayout(cmd_bufs[0], image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dim.z);
	vkCmdCopyBufferToImage(cmd_bufs[0], 临时缓存->m_Data.m_Buf, image->m_Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferCopyRegion);
	//setImageLayout(cmd_bufs[0], image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, dim.z);
	f_vkImage_setLayout(cmd_bufs[0], image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, dim.z);
	//f_vkImage_setLayout(cmd_bufs[0], image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, dim.z);
	
	vkEndCommandBuffer((VkCommandBuffer)cmd_bufs[0]);

	VkSubmitInfo 命令提交信息{ VK_STRUCTURE_TYPE_SUBMIT_INFO };
	命令提交信息.commandBufferCount = 1;
	命令提交信息.pCommandBuffers = (VkCommandBuffer*)&cmd_bufs[0];
	
	//auto result = vkQueueSubmit(配置->m_图形列队[配置->m_列队ID], 1, &命令提交信息, VK_NULL_HANDLE);
	//vkDeviceWaitIdle(配置->m_设备);
	auto result = vkQueueSubmit(配置->m_图形列队[配置->m_列队ID], 1, &命令提交信息, fence);
	do {
		result = vkWaitForFences(配置->m_设备, 1, &fence, VK_TRUE, 100000);
	} while (result == VK_TIMEOUT);
	vkDestroyFence(配置->m_设备, fence, nullptr);

	f_buf_release板载缓存(临时缓存);
	
}

void f_tex_读取纹理数据(S_纹理* tex, S_板载缓存* 临时缓存, S_结构指针 命令缓存) {
	assert(dynamic_cast<S_VkImage*>(tex));
	S_VkImage* image = (S_VkImage*)(tex);

	S_VkArray缓存* GPUBuffer = dynamic_cast<S_VkArray缓存*>(临时缓存);
	VkBufferImageCopy bufferCopyRegion = {};									
	bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;	
	bufferCopyRegion.imageSubresource.mipLevel = 0;								
	bufferCopyRegion.imageSubresource.baseArrayLayer = 0;						
	bufferCopyRegion.imageSubresource.layerCount = tex->m_Size.z;				
	bufferCopyRegion.imageExtent.width = tex->m_Size.x;
	bufferCopyRegion.imageExtent.height = tex->m_Size.y;
	bufferCopyRegion.imageExtent.depth = 1;



	S_VK渲染环境* 配置 = (S_VK渲染环境*)(tex->m_Ctx.m_绘图环境);
	if (!命令缓存) {
		命令缓存 = (S_结构指针)配置->m_命令缓存;
	}

	VkFenceCreateInfo fenceCreateInfo{};
	fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
	fenceCreateInfo.flags = 0;
	VkFence fence;
	vkCreateFence(配置->m_设备, &fenceCreateInfo, nullptr, &fence);
	f_Draw_Begin(&tex->m_Ctx, 命令缓存, (S_结构指针)fence);


	
	VkImageMemoryBarrier barrier = {};
	barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	barrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
	barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
	barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; 
	barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; 
	barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	barrier.image = image->m_Image; // 使用实际的图像对象
	barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
	barrier.subresourceRange.baseMipLevel = 0;
	barrier.subresourceRange.levelCount = 1;
	barrier.subresourceRange.baseArrayLayer = 0;
	barrier.subresourceRange.layerCount = 1;
	vkCmdPipelineBarrier((VkCommandBuffer)命令缓存, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &barrier);
	
	//setImageLayout((VkCommandBuffer)命令缓存, image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 1);
	vkCmdCopyImageToBuffer((VkCommandBuffer)命令缓存, image->m_Image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, GPUBuffer->m_Data.m_Buf, 1, &bufferCopyRegion);
	f_vkImage_setLayout((VkCommandBuffer)命令缓存, image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, 1);

	f_Draw_end(&tex->m_Ctx, 命令缓存, (S_结构指针)fence, 3);
	vkDestroyFence(配置->m_设备, fence, nullptr);

}

void f_tex_转换格式读取纹理数据(S_板载缓存* buf, void* dataPtr, E_纹理格式 源格式, E_纹理格式 目标格式, uint64 num) {
	omp_set_num_threads(4);
	buf->f_map();
	switch (源格式) {
		case E_纹理格式::e_tf_RGBA8U_归一化: {
			switch (目标格式) {
				case E_纹理格式::e_tf_RGBA8U_归一化: {
					break;
				}
				case E_纹理格式::e_tf_RGBA32: {
				#pragma omp parallel for
					for (int64 i = 0; i < num; ++i) {
						((vec4*)dataPtr)[i] = _Vec4(((S_RGBA8UI*)buf->m_mappedPtr)[i]);
					}
					break;
				}
			}
			break;
		}
		case E_纹理格式::e_tf_RGBA32: {
			switch (目标格式) {
				case E_纹理格式::e_tf_RGBA8U_归一化: {
					break;
				}
				case E_纹理格式::e_tf_RGB32: {
				#pragma omp parallel for
					for (int64 i = 0; i < num; ++i) {
						((vec3*)dataPtr)[i] = _Vec3(((vec4*)buf->m_mappedPtr)[i]);
					}
					break;
				}
				case E_纹理格式::e_tf_RGBA32: {
					break;
				}
			}
			break;
		}
		case E_纹理格式::e_tf_R32U:
		case E_纹理格式::e_tf_R32:
		case E_纹理格式::e_tf_D32F: {
			
			break;
		}
	}
	
	buf->f_unmap();
}


void f_tex_setSize(S_纹理* tex, uvec2 newSize) {
	S_VkImage* 图像 = (S_VkImage*)(tex);
	S_VK渲染环境& 配置 = *((S_VK渲染环境*)tex->m_Ctx.m_逻辑设备.渲染环境);
	
	if (图像->m_Size.x == newSize.x && 图像->m_Size.y == newSize.y) return;

	图像->m_Size.x = newSize.x;
	图像->m_Size.y = newSize.y;
	f_df_钳制纹理大小(tex->m_Ctx, 图像->m_Size);


	f_VkImage_构建(图像, 图像->m_Ctx);
	
	auto cmd = f_vk创建命令缓存(&配置);
	f_vk绑定命令缓存(&配置, cmd);
	
	return;
	
	if (图像->m_ImageAspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
		if (图像->m_Cube纹理信息) {
			for (uint32 i = 0; i < 6; ++i) {
				图像->m_Cube纹理信息[i].imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
				f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1, i);
			}
		}
		else {
			f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
		}
		图像->m_纹理信息.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
	}
	else {

		if (图像->m_Cube纹理信息) {
			for (uint32 i = 0; i < 6; ++i) {
				f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1, i);
				图像->m_Cube纹理信息[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
			}
		}
		else {
			f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1);
		}
		图像->m_纹理信息.imageLayout = VK_IMAGE_LAYOUT_GENERAL;

	}

	f_vk刷新命令缓存(&配置, cmd, true);

}

bool f_tex_修改大小(S_纹理* tex, uvec3 newSize, bool 修改内存大小) {
	S_VkImage* 图像 = dynamic_cast<S_VkImage*>(tex);

	if (tex->m_Size.x != newSize.x || tex->m_Size.y != newSize.y || tex->m_Size.z != newSize.z) {
		
		f_df_钳制纹理大小(tex->m_Ctx, newSize);

		tex->m_Size.x = newSize.x;
		tex->m_Size.y = newSize.y;
		tex->m_Size.z = newSize.z;

		if (图像) {
			f_VkImage_构建(图像, 图像->m_Ctx);
		}

		if (tex->m_像素 && 修改内存大小) {
		
			switch (tex->m_格式) {
				case E_纹理格式::e_tf_RGBA8U_归一化: {
					tex->m_像素 = realloc(tex->m_像素, newSize.x * newSize.y * newSize.z * sizeof(S_RGBA8UI));
					break;
				}
				case E_纹理格式::e_tf_RGBA32: {
					tex->m_像素 = realloc(tex->m_像素, newSize.x * newSize.y * newSize.z * sizeof(vec4));
					break;
				}
				case E_纹理格式::e_tf_RGB32: {
					tex->m_像素 = realloc(tex->m_像素, newSize.x * newSize.y * newSize.z * sizeof(vec3));
					break;
				}
				case E_纹理格式::e_tf_R32U:
				case E_纹理格式::e_tf_R32:
				case E_纹理格式::e_tf_D32F: {
					tex->m_像素 = realloc(tex->m_像素, newSize.x * newSize.y * newSize.z * sizeof(float32));
					break;
				}
			}
		}

		//return true;
	}

	
	//vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier);
	if (图像) {
		S_VK渲染环境& 配置 = *((S_VK渲染环境*)(tex->m_Ctx.m_逻辑设备.渲染环境));
		auto cmd = f_vk创建命令缓存(&配置);
		f_vk绑定命令缓存(&配置, cmd);
		

		if (图像->m_ImageAspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
			图像->m_纹理信息.imageLayout = VK_IMAGE_LAYOUT_GENERAL;

			if (图像->m_Cube纹理信息) {
				for (uint32 i = 0; i < 6; ++i) {
					图像->m_Cube纹理信息[i].imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
					f_vkImage_setLayout(cmd, 图像->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1, i);
				}
			}
			else {
				//f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, 1);
				f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
			}

		}
		else {
			f_vkImage_setLayout(cmd, 图像->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1);

			图像->m_纹理信息.imageLayout = VK_IMAGE_LAYOUT_GENERAL;

			switch (tex->m_类型) {
				
				case e_CUBE:
				case e_CUBE_Array: {
					if (图像->m_Cube纹理信息) {
						for (uint32 i = 0; i < 6; ++i) {
							图像->m_Cube纹理信息[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
						}
					}
					break;
				}
				default:
					break;
			}
		}

		
		f_vk刷新命令缓存(&配置, cmd, true);
	}
	return true;
}

void f_tex_拷贝(S_结构指针 cmd, S_纹理* 源, S_纹理* 目标) {
	f_vkImage_setLayout((VkCommandBuffer)cmd, ((S_VkImage*)目标)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1);
	f_vkImage_setLayout((VkCommandBuffer)cmd, ((S_VkImage*)源)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 1);

	VkImageCopy copyRegion{};
	copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
	copyRegion.srcOffset = { 0, 0, 0 };
	copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
	copyRegion.dstOffset = { 0, 0, 0 };
	copyRegion.extent = { 源->m_Size.x, 源->m_Size.y, 1 };
	vkCmdCopyImage((VkCommandBuffer)cmd,
		((S_VkImage*)源)->m_Image,
		VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 
		((S_VkImage*)目标)->m_Image,
		VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 
		1, 
		&copyRegion);

	f_vkImage_setLayout((VkCommandBuffer)cmd, ((S_VkImage*)目标)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 1);
	f_vkImage_setLayout((VkCommandBuffer)cmd, ((S_VkImage*)源)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, 1);
}

void f_tex_拷贝(S_结构指针 cmd, S_纹理* 源, S_板载缓存* 目标) {
	VkImageLayout srcImageLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
	
	VkBufferImageCopy image_copy = {};

	image_copy.bufferOffset = 0;
	image_copy.bufferRowLength = 0;
	image_copy.bufferImageHeight = 0;
	image_copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
	image_copy.imageSubresource.baseArrayLayer = 0;
	image_copy.imageSubresource.layerCount = 1;
	image_copy.imageSubresource.mipLevel = 0;
	image_copy.imageOffset = { 0,0,0 };
	image_copy.imageExtent = { 源->m_Size.x, 源->m_Size.y, 1 };

	vkCmdCopyImageToBuffer((VkCommandBuffer)cmd, ((S_VkImage*)源)->m_Image, srcImageLayout, static_cast<S_VkArray缓存*>(目标)->m_Data.m_Buf, 1, &image_copy);

}

S_纹理* f_tex_创建图像(S_设备环境& ctx, const uvec3& size, E_纹理格式 格式, E_纹理维度类型 类型) {
	S_纹理* tex = new S_纹理();
	tex->m_格式 = 格式;
	tex->m_类型 = 类型;
	tex->m_Size = size;
	tex->m_Ctx = ctx;

	//switch (tex->m_格式) {
	//	case E_纹理格式::e_tf_RGBA8U_U:
	//	case E_纹理格式::e_tf_RGBA32: {
	//		tex->m_通道数量 = 4;
	//		break;
	//	}
	//	case E_纹理格式::e_tf_RGB32: {
	//		tex->m_通道数量 = 3;
	//		break;
	//	}
	//	case E_纹理格式::e_tf_R32U:
	//	case E_纹理格式::e_tf_R32:
	//	case E_纹理格式::e_tf_D32F: {
	//		tex->m_通道数量 = 1;
	//		break;
	//	}
	//}
	tex->m_通道数量 = f_tex_格式通道数量(tex->m_格式);

	return tex;
}

uint32 f_tex_格式通道数量(E_纹理格式 格式) {
	switch (格式) {
		case E_纹理格式::e_tf_RGBA8U_归一化:
		case E_纹理格式::e_tf_RGBA32: {
			return 4;
		}
		case E_纹理格式::e_tf_RGB32: {
			return 3;
		}
		case E_纹理格式::e_tf_R32U:
		case E_纹理格式::e_tf_R32:
		case E_纹理格式::e_tf_D32F: {
			return 1;
		}
	}
	return 1;
}

uvec3 f_tex_分辨率(S_纹理* tex) {
	return tex->m_Size;
}



// 转换图像布局
static void f_tex_transitionImageLayout(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout) {
	VkImageMemoryBarrier barrier{};
	barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	barrier.oldLayout = oldLayout;
	barrier.newLayout = newLayout;
	barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	barrier.image = image;
	barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
	barrier.subresourceRange.baseMipLevel = 0;
	barrier.subresourceRange.levelCount = 1;
	barrier.subresourceRange.baseArrayLayer = 0;
	barrier.subresourceRange.layerCount = 1;

	VkPipelineStageFlags sourceStage;
	VkPipelineStageFlags destinationStage;

	if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
		barrier.srcAccessMask = 0;
		barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
		sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
		destinationStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
	} 
	else if (oldLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
		barrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
		barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
		sourceStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
		destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
	} 
	else {
		throw std::invalid_argument("Unsupported layout transition!");
	}

	vkCmdPipelineBarrier(
		commandBuffer,
		sourceStage, destinationStage,
		0,
		0, nullptr,
		0, nullptr,
		1, &barrier
	);
}

void f_tex_渲染布局(S_纹理* tex) {
	S_VkImage* vkimage = static_cast<S_VkImage*>(tex);
	auto* 绘图环境 = f_vk_get绘图环境(vkimage->m_Ctx);

	f_tex_transitionImageLayout(绘图环境->m_命令缓存, vkimage->m_Image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);

}

void f_tex_采样布局(S_纹理* tex) {
	assert(dynamic_cast<S_VkImage*>(tex));
	S_VkImage& image = *(S_VkImage*)(tex);



	S_VK渲染环境* 配置 = (S_VK渲染环境*)(tex->m_Ctx.m_绘图环境);
	VkCommandBuffer cmd_bufs[1] = { };
	cmd_bufs[0] = 配置->m_命令缓存;


	VkFenceCreateInfo fenceCreateInfo{};
	fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
	fenceCreateInfo.flags = 0;
	VkFence fence;
	vkCreateFence(配置->m_设备, &fenceCreateInfo, nullptr, &fence);


	static VkCommandBufferInheritanceInfo InheritanceInfo{};
	InheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
	vkResetCommandBuffer(cmd_bufs[0], 0);
	VkCommandBufferBeginInfo cmdBufferBeginInfo{};
	cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
	cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
	cmdBufferBeginInfo.pInheritanceInfo = &InheritanceInfo;
	auto err = vkBeginCommandBuffer(cmd_bufs[0], &cmdBufferBeginInfo);

	f_vkImage_setLayout(cmd_bufs[0], image.m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1, 0, image.m_Mip);

	vkEndCommandBuffer((VkCommandBuffer)cmd_bufs[0]);

	VkSubmitInfo 命令提交信息{ VK_STRUCTURE_TYPE_SUBMIT_INFO };
	命令提交信息.commandBufferCount = 1;
	命令提交信息.pCommandBuffers = (VkCommandBuffer*)&cmd_bufs[0];

	auto result = vkQueueSubmit(配置->m_图形列队[配置->m_列队ID], 1, &命令提交信息, fence);
	do {
		result = vkWaitForFences(配置->m_设备, 1, &fence, VK_TRUE, 100000);
	} while (result == VK_TIMEOUT);
	vkDestroyFence(配置->m_设备, fence, nullptr);
}

void f_tex_读写布局(S_纹理* tex) {
	assert(dynamic_cast<S_VkImage*>(tex));
	S_VkImage& image = *(S_VkImage*)(tex);

	S_VK渲染环境* 配置 = (S_VK渲染环境*)(tex->m_Ctx.m_绘图环境);
	VkCommandBuffer cmd_bufs[1] = { };
	cmd_bufs[0] = 配置->m_命令缓存;


	VkFenceCreateInfo fenceCreateInfo{};
	fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
	fenceCreateInfo.flags = 0;
	VkFence fence;
	vkCreateFence(配置->m_设备, &fenceCreateInfo, nullptr, &fence);


	static VkCommandBufferInheritanceInfo InheritanceInfo{};
	InheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
	vkResetCommandBuffer(cmd_bufs[0], 0);
	VkCommandBufferBeginInfo cmdBufferBeginInfo{};
	cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
	cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
	cmdBufferBeginInfo.pInheritanceInfo = &InheritanceInfo;
	auto err = vkBeginCommandBuffer(cmd_bufs[0], &cmdBufferBeginInfo);

	f_vkImage_setLayout(cmd_bufs[0], image.m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1, 0, image.m_Mip);

	vkEndCommandBuffer((VkCommandBuffer)cmd_bufs[0]);

	VkSubmitInfo 命令提交信息{ VK_STRUCTURE_TYPE_SUBMIT_INFO };
	命令提交信息.commandBufferCount = 1;
	命令提交信息.pCommandBuffers = (VkCommandBuffer*)&cmd_bufs[0];

	auto result = vkQueueSubmit(配置->m_图形列队[配置->m_列队ID], 1, &命令提交信息, fence);
	do {
		result = vkWaitForFences(配置->m_设备, 1, &fence, VK_TRUE, 100000);
	} while (result == VK_TIMEOUT);
	vkDestroyFence(配置->m_设备, fence, nullptr);
}



void f_tex_深度渲染布局(S_纹理** tex, uint32 num) {
	auto dc = f_vk_get绘图环境(tex[0]->m_Ctx);
	
	auto cmd = f_vk创建命令缓存(dc);
	f_vk绑定命令缓存(dc, cmd);

	for (uint32 i = 0; i < num; ++i) {
		auto 图像 = static_cast<S_VkImage*>(tex[i]);
		f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 1);
	}
	
	f_vk刷新命令缓存(dc, cmd, true);
}

void f_tex_深度采样布局(S_纹理** tex, uint32 num) {
	auto dc = f_vk_get绘图环境(tex[0]->m_Ctx);

	auto cmd = f_vk创建命令缓存(dc);
	f_vk绑定命令缓存(dc, cmd);

	for (uint32 i = 0; i < num; ++i) {
		auto 图像 = static_cast<S_VkImage*>(tex[i]);
		f_vkImage_setLayout(cmd, 图像->m_Image, 图像->m_ImageAspect, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, 1);
	}

	f_vk刷新命令缓存(dc, cmd, true);
}




void f_tex_拷贝纹理(S_设备环境& ctx, S_纹理* des, S_纹理* src, const std::vector<uvec2>& 区域) {
	auto 图像D = static_cast<S_VkImage*>(des);
	auto 图像S = static_cast<S_VkImage*>(src);

	// 源图像：转换为 TRANSFER_SRC_OPTIMAL
	/*
	VkImageMemoryBarrier srcBarrier{};
	srcBarrier.sType			= VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	srcBarrier.oldLayout		= VK_IMAGE_LAYOUT_GENERAL;
	srcBarrier.newLayout		= VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
	srcBarrier.srcAccessMask	= VK_ACCESS_MEMORY_WRITE_BIT;  // 根据实际前序操作调整
	srcBarrier.dstAccessMask	= VK_ACCESS_TRANSFER_READ_BIT;
	srcBarrier.image			= 图像S->m_Image;
	srcBarrier.subresourceRange	= {
		.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
		.baseMipLevel = 0,
		.levelCount = 1,
		.baseArrayLayer = 0,
		.layerCount = 1
	};

	// 目标图像：转换为 TRANSFER_DST_OPTIMAL
	VkImageMemoryBarrier dstBarrier{};
	dstBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	dstBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;  // 假设原布局为 GENERAL
	dstBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
	dstBarrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;  // 根据实际前序操作调整
	dstBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
	dstBarrier.image = dstImage;
	dstBarrier.subresourceRange = {
		.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
		.baseMipLevel = 0,
		.levelCount = 1,
		.baseArrayLayer = 0,
		.layerCount = 1
	};

	// 插入屏障到命令缓冲（确保管线阶段正确）
	vkCmdPipelineBarrier(
		cmdBuf,
		VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,  // 源阶段（根据实际情况调整）
		VK_PIPELINE_STAGE_TRANSFER_BIT,      // 目标阶段（传输操作）
		0,
		0, nullptr,
		0, nullptr,
		2,  // 两个屏障（源和目标）
		&srcBarrier, &dstBarrier
	);


	VkImageCopy copyRegion{};
	// 源图像设置
	copyRegion.srcSubresource = {
		.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
		.mipLevel = 0,
		.baseArrayLayer = 0,
		.layerCount = 1
	};
	copyRegion.srcOffset = {100, 100, 0};  // 源图像起始坐标 (x, y, z)
	copyRegion.extent = {200, 150, 1};     // 复制区域大小 (宽, 高, 深)

	// 目标图像设置
	copyRegion.dstSubresource = {
		.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
		.mipLevel = 0,
		.baseArrayLayer = 0,
		.layerCount = 1
	};
	copyRegion.dstOffset = {50, 50, 0};    // 目标图像起始坐标

	// 3. 在已有命令缓冲中录制复制命令
	vkCmdCopyImage(
		cmdBuf,
		srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,  // 源图像布局（必须匹配）
		dstImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,  // 目标图像布局（必须匹配）
		1,  // 复制区域数量
		&copyRegion
	);
	*/
}




CORE_ARRAY_HANDLE(S_纹理信息, VkDescriptorImageInfo)
typedef struct {
	S_纹理信息* 纹理信息;
	S_纹理组*	纹理对象;
}S_Vk纹理集;


S_纹理集* f_纹理集_创建(uint32 num) {
	S_Vk纹理集* ts = (S_Vk纹理集*)malloc(sizeof(S_Vk纹理集));

	ts->纹理对象 = (S_纹理组*)f_core_array_new(num, sizeof(S_纹理*));
	ts->纹理信息 = (S_纹理信息*)f_core_array_new(num, sizeof(VkDescriptorImageInfo));

	return (S_纹理集*)ts;
}

void f_纹理集_销毁(S_纹理集* ts) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);
	f_core_array_free((S_Array*)vkTs.纹理对象);
	f_core_array_free((S_Array*)vkTs.纹理信息);
	free(&vkTs);
}


uint32 f_纹理集_add纹理(S_纹理集* ts, S_纹理* 纹理) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);
	f_core_array_push_back((S_Array*)vkTs.纹理对象, (uint8*)&(纹理) );
	f_core_array_push_back((S_Array*)vkTs.纹理信息, (uint8*)&( ((S_VkImage*)纹理)->m_纹理信息 ) );
	return vkTs.纹理信息->count;
}

uint32 f_纹理集_find纹理(S_纹理集* ts, const S_纹理* 纹理) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);
	int32 id = f_core_array_find((S_Array*)vkTs.纹理对象, (const uint8*)&纹理);
	return id;
}

uint32 f_纹理集_get数量(S_纹理集* ts) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);
	return ts->data->count;
}

void f_纹理集_set纹理(S_纹理集* ts, S_纹理* 纹理, uint32 loc) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);
	assert(loc < vkTs.纹理对象->count);
	vkTs.纹理对象->ptr_userData[loc] = 纹理;
	vkTs.纹理信息->ptr_userData[loc] = ((S_VkImage*)纹理)->m_纹理信息;
}

void f_纹理集_rem纹理(S_纹理集* ts, S_纹理* 纹理) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);

	for (uint32 i = 0; i < vkTs.纹理对象->count; ++i) {
		if (vkTs.纹理对象->ptr_userData[i] == 纹理) {
			f_core_array_erase((S_Array*)vkTs.纹理对象, i);
			f_core_array_erase((S_Array*)vkTs.纹理信息, i);
			return;
		}
	}
}

void f_纹理集_set数量(S_纹理集* ts, uint32 num) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);

	f_core_array_resize((S_Array*)vkTs.纹理对象, num);
	f_core_array_resize((S_Array*)vkTs.纹理信息, num);
}

void f_纹理集_fill纹理(S_纹理集* ts, S_纹理** 纹理, uint32 num) {
	f_纹理集_set数量(ts, num);
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);

	for(uint32 i=0; i<num; ++i) f_纹理集_set纹理(ts, 纹理[i], i);
}

void f_纹理集_重置纹理大小(S_纹理集* ts, uint32 loc, uvec2 size) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)ts);
	if(vkTs.纹理对象->count <= loc) return;

	f_tex_setSize(vkTs.纹理对象->ptr_userData[loc], size);
}

void f_纹理集_刷新所有纹理信息(S_纹理集* ts) {
	S_Vk纹理集& 纹理集 = *((S_Vk纹理集*)ts);

	for (uint32 i = 0; i < 纹理集.纹理对象->count; ++i) {
		纹理集.纹理信息->ptr_userData[i] = ((S_VkImage*)纹理集.纹理对象->ptr_userData[i])->m_纹理信息;
	}
}

S_纹理* f_纹理集_get纹理(S_纹理集* ts, uint32 loc) {
	S_Vk纹理集& 纹理集 = *((S_Vk纹理集*)ts);
	if (loc < 纹理集.纹理对象->count) {
		return 纹理集.纹理对象->ptr_userData[loc];
	}
	return nullptr;
}




S_GPU纹理参数槽 f_纹理槽_创建(uint32 num) {
	S_GPU纹理参数槽 纹理槽;
	纹理槽.m_纹理信息 = (void**)calloc(num, sizeof(void*));
	纹理槽.m_绑定位置 = (uint32*)calloc(num, sizeof(uint32));
	纹理槽.m_绑定数量 = (uint32*)calloc(num, sizeof(uint32));
	纹理槽.m_槽数量 = num;
	return 纹理槽;
}

void f_纹理槽_销毁(S_GPU纹理参数槽& 纹理槽) {
	free(纹理槽.m_纹理信息);
	free(纹理槽.m_绑定位置);
	free(纹理槽.m_绑定数量);
	纹理槽.m_槽数量 = 0;

	纹理槽 = {};
}

void f_纹理槽_设置纹理(S_GPU纹理参数槽& 纹理槽, S_纹理* tex, uint32 绑定位置, uint32 槽) {
	if(纹理槽.m_槽数量 == 0) return;
	if(槽 >= 纹理槽.m_槽数量) 槽 = 纹理槽.m_槽数量 - 1;

	纹理槽.m_纹理信息[槽] = &(static_cast<S_VkImage*>(tex)->m_纹理信息);
	纹理槽.m_绑定位置[槽] = 绑定位置;
	纹理槽.m_绑定数量[槽] = 1;
}

void f_纹理槽_填充纹理集(S_GPU纹理参数槽& 纹理槽, uint32 num, S_纹理集** ts, uint32 bindID) {
	if (纹理槽.m_槽数量 != num) {
		纹理槽.m_纹理信息 = (void**)realloc(纹理槽.m_纹理信息, num * sizeof(void*));
		纹理槽.m_绑定位置 = (uint32*)realloc(纹理槽.m_绑定位置, num * sizeof(uint32));
		纹理槽.m_绑定数量 = (uint32*)realloc(纹理槽.m_绑定数量, num * sizeof(uint32));
		纹理槽.m_槽数量 = num;
	}

	for (uint32 i = 0; i < num; ++i) {
		f_纹理集_刷新所有纹理信息(ts[i]);
		f_纹理槽_设置纹理集(纹理槽, ts[i], i, bindID);
	}
}

void f_纹理槽_设置纹理集(S_GPU纹理参数槽& 纹理槽, S_纹理集* 纹理集, uint32 loc, uint32 bindID) {
	S_Vk纹理集& vkTs = *((S_Vk纹理集*)纹理集);

	纹理槽.m_纹理信息[loc] = vkTs.纹理信息->ptr_userData;
	纹理槽.m_绑定数量[loc] = vkTs.纹理信息->count;
	纹理槽.m_绑定位置[loc] = bindID;
}

void f_纹理槽_更新纹理集(S_GPU纹理参数槽& 纹理槽, uint32 num, S_纹理集** 纹理集) {
	for (uint32 i = 0; i < num; ++i) {
		f_纹理槽_设置纹理集(纹理槽, 纹理集[i], i, 纹理槽.m_绑定位置[i]);
	}
}




